<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>编辑商品（报损报溢）</title>
		<link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
		<link rel="stylesheet" href="../../css/public.css" media="all">
	</head>
	<body>
		<form class="layui-form" id="editProduct" lay-filter="editProduct">
			<input type="hidden" id="breakageProInfo">
			<!--单据类型-->
			<input type="hidden" id="proInvType">
			<!-- 商品信息 -->
			<div class="layui-row layui-panel" style="padding:15px">
				<fieldset class="table-search-fieldset" style="border: 1px solid #eee ;">
					<legend>商品信息</legend>
					<!-- 基本信息 -->
					<div class="layui-row">
						<div class="layui-col-md6">
							<label class="layui-form-label">商品编号：</label>
							<div class="layui-input-block">
								<input class="layui-input" name="proId" style="color:blue;border: none;" readonly />
							</div>
						</div>
						<div class="layui-col-md6">
							<label class="layui-form-label">商品名称：</label>
							<div class="layui-input-block">
								<input class="layui-input" name="inveDescName" style="color:red;border: none;" readonly />
							</div>
						</div>
						<div class="layui-col-md6">
							<label class="layui-form-label">规格型号：</label>
							<div class="layui-input-block">
								<input class="layui-input" name="inveDescSize" style="color:blue;border: none;" readonly />
							</div>
						</div>
						<div class="layui-col-md6">
							<label class="layui-form-label">单位：</label>
							<div class="layui-input-block">
								<input class="layui-input" name="inveDescUnit" style="color:blue;border: none;" readonly />
							</div>
						</div>
						<div class="layui-col-md6">
							<label class="layui-form-label">当前库存：</label>
							<div class="layui-input-block">
								<input class="layui-input" name="inveDescInventoryNum" style="color:red;border: none;" readonly />
							</div>
						</div>
						<div class="layui-col-md6">
							<label class="layui-form-label">批准文号：</label>
							<div class="layui-input-block">
								<input class="layui-input" name="inveDescPostil" style="color:blue;border: none;" readonly />
							</div>
						</div>
						<div class="layui-col-md6">
							<label class="layui-form-label">生产厂商：</label>
							<div class="layui-input-block">
								<input class="layui-input" name="inveDescManufacturer" style="color:blue;border: none;" readonly />
							</div>
						</div>
						<div class="layui-col-md6">
							<label class="layui-form-label">备注：</label>
							<div class="layui-input-block">
								<input class="layui-input" name="inveDescComment" style="color:blue;border: none;" readonly />
							</div>
						</div>
					</div>
					<!-- 可更改的信息 -->
					<div class="layui-row" style="padding: 10px; border-top: 1px solid #eee ;">
						<div class="layui-col-md6">
							<div class="layui-form-item">
								<label class="layui-form-label">数量：</label>
								<div class="layui-input-block">
									<input type="text" name="proNum" required lay-verify="required" autocomplete="off" placeholder="数量不能为空" class="layui-input" value="1">
								</div>
							</div>
						</div>
						
						<div class="layui-col-md6">
							<div class="layui-form-item">
								<label class="layui-form-label">产品批号：</label>
								<div class="layui-input-block">
									<input type="text" name="inveDescBatchNum" autocomplete="off" placeholder=""
										   class="layui-input" readonly style="cursor: not-allowed ;border: none">
								</div>
							</div>
						</div>
						
						<div class="layui-col-md6">
							<div class="layui-form-item">
								<label class="layui-form-label">生产日期：</label>
								<div class="layui-input-block">
									<input class="layui-input" name="inveDescStartDate" style="border: none;" readonly />
								</div>
							</div>
						</div>

						<div class="layui-col-md6">
							<div class="layui-form-item">
								<label class="layui-form-label">有效期：</label>
								<div class="layui-input-block">
									<input class="layui-input" name="inveDescValidityDate" style="border: none;" readonly />
								</div>
							</div>
						</div>
						
						<div class="layui-col-md12">
							<div class="layui-form-item">
								<label class="layui-form-label">备注：</label>
								<div class="layui-input-block">
									<input class="layui-input" name="addComment" placeholder="请输入相关备注">
								</div>
							</div>
						</div>
					</div>
				</fieldset>
				<div class="layui-col-md12" style="text-align: center; margin-top: 20px;">
					<div class="layui-btn-container">
					  <button type="button" class="layui-btn layui-btn-sm" lay-submit lay-filter="saveBtn">确定</button>
					</div>
				</div>
			</div>
			<!--进价-->
			<input type="hidden" name="inveDescPrice">
			<!--进价总额-->
			<input type="hidden" name="inveDescTotalPrice">
			<!--预设售价-->
			<input type="hidden" name="invePresetPrice">
			<!--售价总金额-->
			<input type="hidden" name="invePresetTotalPrice">
		</form>
		
		<!-- 商品批次信息-->
		<div class="layui-row layui-panel" style="padding: 10px 15px; margin-top:15px;">
			<div class="layui-tab">
				<ul class="layui-tab-title">
					<li class="layui-this">商品批次信息</li>
				</ul>
				<div class="layui-tab-content">
					<!-- 商品批次信息 -->
					<div class="layui-tab-item layui-show">
						<fieldset class="table-search-fieldset" style="border: 1px solid #eee ;">
							<legend>可以在下面列表中选择不同批次的商品</legend>
							<table class="layui-hide" id="batchInfo" lay-filter="batchInfo"></table>
						</fieldset>
					</div>
				</div>
			</div>
		</div>
		
		<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
		<script src="../../lib/jquery-3.4.1/jquery-3.4.1.min.js"></script>
		<script>
			$(function () {
                layui.use(['form', 'table', 'layer','tree','element'], function() {
                    var $ = layui.jquery,
                        form = layui.form,
                        table = layui.table,
                        layer = layui.layer,
                        tree = layui.tree;

                    //获取商品信息(String对象 --> JSON格式的对象)
					var data = JSON.parse(parent.breakageProdata);

                    //给表单填充值
                    form.val('editProduct',data);

                    // 商品批次信息
                    table.render({
                        elem: '#batchInfo',
                        url: '/searchProBatchNumInfo/'+data.proId+'?inveDescDepot='+ parent.selectDescDepot +'',
                        totalRow: true , //开启合计行
                        cellMinWidth: 120,
                        defaultToolbar: ['filter', 'exports', 'print', {
                            title: '提示',
                            layEvent: 'LAYTABLE_TIPS',
                            icon: 'layui-icon-tips'
                        }],
                        cols: [[
                            {field: 'proId', title: '商品编号', sort: true, align:"center"},
                            {field: 'inveDescName', title: '商品名称', align:"center"},
                            {field: 'purStoDate', title: '进货日期', sort: true, align:"center",
								templet:"<div>{{layui.util.toDateString(d.purStoDate, 'yyyy-MM-dd')}}</div>"},
                            {field: 'inveDescInventoryNum', title: '库存数量', sort: true, totalRow: true, align:"center"},
                            {field: 'inveDescBatchNum', title: '产品批号', align:"center"},
                            {field: 'inveDescStartDate', title: '生产日期', sort: true, align:"center",
                                templet:"<div>{{layui.util.toDateString(d.inveDescStartDate, 'yyyy-MM-dd')}}</div>"},
                            {field: 'inveDescValidityDate', title: '有效期至', sort: true, align:"center",
                                templet:"<div>{{layui.util.toDateString(d.inveDescValidityDate, 'yyyy-MM-dd')}}</div>"}
                        ]],
                        done: function(obj){
                            var tableIndex;
							for(var i = 0;i < obj.data.length;i++){
							    if(obj.data[i].inveDescBatchNum == $("input[name = 'inveDescBatchNum']").val()){
                                    tableIndex = i;
								}
							}
                            $('.layui-table-view[lay-id="batchInfo"]')
                                .children('.layui-table-box')
                                .children('.layui-table-body')
                                .find('table tbody tr[data-index='+ tableIndex +']').click();
                        },
                        limits: [10, 15, 20, 25, 50, 100],
                        limit: 15,
                        page: false,
                        skin: 'line'
                    });

                    //编辑批号和改变不同批号的库存量
                    var selectProBatchNum;
                    table.on('row(batchInfo)', function(obj){
                        selectProBatchNum = obj.data;
                        obj.tr.addClass('layui-bg-green').siblings().removeClass('layui-bg-green');
                        $("input[name='inveDescBatchNum']").val(selectProBatchNum.inveDescBatchNum);
                        $("input[name='inveDescInventoryNum']").val(selectProBatchNum.inveDescInventoryNum);
                        $("input[name='inveDescStartDate']").val(getFormatDate(selectProBatchNum.inveDescStartDate));
                        $("input[name='inveDescValidityDate']").val(getFormatDate(selectProBatchNum.inveDescValidityDate));
                    });

                    //监听提交（保存修改的商品信息）
                    form.on('submit(saveBtn)', function (obj) {
                        // 输入数量
                        var inputNum = $("input[name='proNum']");
                        //定义一个布尔类型判断输入的数量是否规范
						var inputNumIsFlag = false;
                        switch ($("#proInvType").val()){
                            case "报损":
                                //库存量
                                var inveDescInventoryNum =+$("input[name='inveDescInventoryNum']").val();
                                if(inveDescInventoryNum < inputNum.val()||isNaN(inputNum.val())||0 >= inputNum.val()){
                                    inputNum.val('');
                                    inputNum.focus();
                                    layer.msg('请输入大于0小于'+inveDescInventoryNum+'的数！',{time:1200,shift:6},function () {
                                    });
                                }else {
                                    inputNumIsFlag = true;
								}
                                break;
                            case "报溢":
                                if(0 >= inputNum.val()||isNaN(inputNum.val())){
                                    inputNum.val('');
                                    inputNum.focus();
                                    layer.msg('请输入大于0的数！',{time:1200,shift:6},function () {
                                    })
                                }else {
                                    inputNumIsFlag = true;
                                }
                                break;
                        }

                        if(inputNumIsFlag){
                            // 声明保存后刷新的数据表格和填充数据
                            var tableFilter = 'selectProInfo';
                            var tableData = parent.chooseBreakInfoList;

                            // 判断刷新的数据表格
                            if(parent.editProductFlag){
                                tableFilter = 'comBreakOverflow';
                                tableData = parent.chooseBreakInfoLists;
                            }

                            //声明一个变量判断是否符合符合添加的条件
                            var isSamePro = true;

                            var chooseProInfo = JSON.parse(JSON.stringify(obj.field));
                            //计算进价总额
                            chooseProInfo.inveDescTotalPrice = chooseProInfo.proNum * chooseProInfo.inveDescPrice;
                            //计算售价总金额
                            chooseProInfo.invePresetTotalPrice = chooseProInfo.proNum * chooseProInfo.invePresetPrice;

                            //循环判断
                            for(var i = 0,len = tableData.length;i<len;i++){
                                var proInfo = tableData[i];
                                //数组中有批号、编号相同的商品则修改数量
                                if(proInfo.inveDescBatchNum === chooseProInfo.inveDescBatchNum
                                    && proInfo.proId === chooseProInfo.proId){
                                    //修改数量
                                    if(parent.editProductFlag != undefined){
                                        proInfo.proNum = chooseProInfo.proNum;
                                        proInfo.addComment = chooseProInfo.addComment;
                                    }else {
                                        proInfo.proNum = +proInfo.proNum + +chooseProInfo.proNum;
                                    }
                                    //更改进价总额
                                    proInfo.inveDescTotalPrice = proInfo.proNum * chooseProInfo.inveDescPrice;
                                    //更改售价总金额
                                    proInfo.invePresetTotalPrice = proInfo.proNum * chooseProInfo.invePresetPrice;
                                    //更改判断条件
                                    isSamePro = false;
                                    //退出循环
                                    break;
                                }
                            }

                            //数组中没有相同批号和编号则添加
                            if(isSamePro){
                                //将修改的商品信息添加至所选商品数组中
                                tableData.push(chooseProInfo);
                            }

                            // 刷新父窗体所选商品信息表格
                            //parent.layui.table <==> parent.table + 父窗体配置window.table = tableList[3];
                            parent.table.reload(tableFilter,{
                                // 填充父窗体所选商品信息数据表格(selectProInfo)的信息data
                                data : tableData
                            });

                            //关闭弹窗
                            var iframeIndex = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(iframeIndex);
						}
                    });

                    /* ---------- 日期格式的函数 ---------- */
                    function getFormatDate(obj) {
                        var date = new Date(obj);

                        var year = date.getFullYear();
                        var month = date.getMonth()+1;
                        var day = date.getDate();

                        month = month<10 ? "0"+month : month;
                        day = day<10 ? "0"+day : day;

                        return year + "-" + month + "-" + day;
                    };
                });
            });
		</script>
	</body>
</html>
